<template>
    <div class="comment-item">
        <div class="comment-header">
            <el-avatar :src="comment.userAvatar" />
            <span class="author">{{ comment.userNickname }}</span>
            <span class="time">{{ formatTime(comment.createTime) }}</span>
        </div>
        <div class="comment-content">{{ commentView }}</div>

        <!-- 回复按钮和子评论 -->
        <div class="comment-actions">
            <el-button size="small" @click="$emit('reply', comment)">回复</el-button>
        </div>
    </div>
</template>

<script setup>
import { defineProps, ref, watch } from 'vue';
import { computed } from 'vue';

const props = defineProps({
    avatar: {
        type: String,
    },
    comment: {
        type: Object,
        required: true
    },
    name: {
        type: String
    }
});

watch(() => props.article, (newComment) => {
    comment = newComment;
}, { deep: true });

const comment = props.comment;

const formatTime = (time) => {
    return new Date(time).toLocaleString();
};

const emit = defineEmits(['reply', 'view-floor']);

const commentView = computed(() => {
    if (comment.parentUserNickname) {
        return `@${comment.parentUserNickname} : ${ comment.content}`;
    } else {
        return comment.content;
    }
})

</script>

<style scoped>
.comment-item {
    padding: 10px;
    margin: 10px 0;
    border-radius: 4px;
    background: #f8f9fa;
}

.comment-header {
    display: flex;
    align-items: center;
    gap: 8px;
    color: #666;
    margin-bottom: 5px;
}

.author {
    font-weight: 500;
}

.time {
    font-size: 0.9em;
    color: #999;
}

.comment-content {
    margin-left: 28px;
}

.replies {
    margin-left: 30px;
    border-left: 2px solid #eee;
    padding-left: 15px;
}

.comment-actions {
    margin-top: 8px;
    margin-left: 28px;
}
</style>
